## creates table for gaps controlling for race
## table 4

rm(list=ls())

library(tidyverse)
library(kableExtra)


# read in data

issuetopics <- read.csv('issuetopics.csv')

opinion_racectrl <- readRDS('bootstrap_gaps_data/opinion_with_racectrls.RDS')



# table 4 - opinion gaps controlling for race

polarization_tab <- data.frame(expand.grid(party = c('rep', 'dem', 'all'),
                                           topic_6 = unique(issuetopics$topic_6),
                                           stringsAsFactors = FALSE),
                               avgpol = NA,
                               signif = NA,
                               gilens = NA,
                               gilenssignif = NA,
                               disagree = NA,
                               n_quest = NA,
                               stringsAsFactors = FALSE)

for (i in 1:nrow(polarization_tab)) {
  temp.issues <- unique(issuetopics$question[issuetopics$topic_6 == polarization_tab$topic_6[i]])
  
  temp.df <- opinion_racectrl %>% 
    filter(topic_6 == polarization_tab$topic_6[i] & party == polarization_tab$party[i])
  
  polarization_tab$avgpol[i] <- mean(abs(temp.df$coef))
  
  polarization_tab$signif[i] <- sum(abs(temp.df$coef) > (temp.df$se*1.96))
  
  polarization_tab$gilens[i] <- sum(abs(temp.df$coef) > 0.1)
  
  
  polarization_tab$n_quest[i] <- nrow(temp.df)
}

for (p in unique(polarization_tab$party)) {
  i <- nrow(polarization_tab) + 1
  polarization_tab[i,] <- NA
  polarization_tab$topic_6[i] <- 'Total'
  polarization_tab$party[i] <- p
  
  temp.df <- opinion_racectrl %>% 
    filter(party == polarization_tab$party[i])
  
  polarization_tab$avgpol[i] <- mean(abs(temp.df$coef))
  
  for (v in c('signif', 'gilens', 'gilenssignif', 'disagree', 'n_quest')) { 
    polarization_tab[i,v] <- sum(polarization_tab[polarization_tab$party == p, v], na.rm = TRUE)
  }
}

polarization_tab %>% 
  select(party, topic_6, n_quest, avgpol, signif, gilens) %>% 
  arrange(party) %>%
  mutate(party = case_when(party == 'rep' ~ 'Republicans',
                           party == 'dem' ~ 'Democrats',
                           party == 'all' ~ 'All Respondents'),
         topic_6 = case_when(topic_6 == 'foreignpolicy' ~ 'Foreign policy',
                             topic_6 == 'cultural' ~ 'Cultural',
                             topic_6 == 'immigration' ~ 'Immigration',
                             topic_6 == 'lawenforcement' ~ 'Law enforcement',
                             topic_6 == 'economic' ~ 'Economic',
                             topic_6 == 'socialwelfare' ~ 'Social welfare',
                             TRUE ~ topic_6)) %>% 
  mutate_at(vars(signif:gilens), funs(. / n_quest)) %>% 
  mutate_if(is.numeric, round, 3) %>% 
  mutate_at(vars(signif:gilens), funs(paste0(. * 100, '\\%'))) %>% 
  mutate(party = cell_spec(party, angle = 90, format = 'latex')) %>% 
  kable('latex', booktabs = TRUE, escape = FALSE, linesep = '\\addlinespace',
        col.names = linebreak(c(' ', 'Policy\nDomain', 'Number of\nQuestions', 'Average\nOpinion Gap', 'Opinion Difference\nStatistically\nSignificant', 
                                'Opinion Gap\nGreater than\n10 pts.'),
                              align = 'c'),
        align = c('l', 'l', rep('c', ncol(.)-2))) %>%
  collapse_rows(1, latex_hline = 'major') %>% 
  row_spec(c(7, 14, 21), bold = TRUE) %>% 
  save_kable(file = 'tables/detailed_polarization_racecontrol.tex')
